package com.hm.hall.commons.util;


import com.alibaba.fastjson.JSONObject;
import com.hm.hall.commons.service.ConsoleConfigService;
import com.hm.hall.commons.service.RedisCacheService;
import org.apache.commons.lang.StringUtils;


/**
 * 配置项工具类  ( 经过缓存处理的.  随便用)
 */
public class ConsoleConfigUtils {
    static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ConsoleConfigUtils.class);
    static ConsoleConfigService configService;
    static RedisCacheService redisCacheService;

    /**
     * 根据ckey值获取字符串类型配置项
     *
     * @param ckey ckey
     * @return val
     */
    public static String getStringConfig(String ckey) {
        return getConfigService().getValueByCache(ckey);
    }

    // 文件转换
    public static String changeToHtml(String text) {
        if (StringUtils.isBlank(text)) {
            return text;
        }
        text = text.replace("&amp;", "&");
        text = text.replace("&nbsp;", " ");
        text = text.replace("&lt;", "<");
        text = text.replace("&gt;", ">");
        text = text.replace("&quot;", "\"");
        text = text.replace("&ldquo;", "\"");
        text = text.replace("&rdquo;", "\"");
        return text;
    }

    /**
     * 根据ckey值获取字符串类型配置项
     *
     * @param ckey   ckey
     * @param defVal 默认值
     * @return val
     */
    public static String getStringConfig(String ckey, String defVal) {
        String val = getStringConfig(ckey);
        if (null == val) {
            val = defVal;
        }
        return val;
    }

    /**
     * 根据ckey值获取布尔类型配置项
     *
     * @param ckey ckey
     * @return val
     */
    public static Boolean getBooleanConfig(String ckey) {
        String configVal = getStringConfig(ckey);
        if (StringUtils.isEmpty(configVal)) {
            return null;
        }
        Boolean result = null;
        try {
            result = Boolean.parseBoolean(configVal);
        } catch (Exception e) {
            logger.error(ExceptionUtils.getErrorMessageWithNestedException(e));
        }
        return result;
    }

    /**
     * 根据ckey值获取布尔类型配置项
     *
     * @param ckey     ckey
     * @param defValue defValue
     * @return val
     */
    public static Boolean getBooleanConfig(String ckey, boolean defValue) {
        Boolean objBoolean = getBooleanConfig(ckey);
        if (objBoolean == null) {
            return defValue;
        }
        return objBoolean;
    }

    /**
     * 根据ckey值获取Integer类型配置项
     *
     * @param ckey ckey
     * @return val
     */
    public static Integer getIntegerConfig(String ckey) {
        String configVal = getStringConfig(ckey);
        if (StringUtils.isEmpty(configVal)) {
            return null;
        }
        Integer ival = null;
        try {
            ival = Integer.parseInt(configVal);
        } catch (NumberFormatException e) {
            logger.error(ExceptionUtils.getErrorMessageWithNestedException(e));
        }
        return ival;
    }

    /**
     * 根据ckey值获取Integer类型配置项
     *
     * @param ckey ckey
     * @return val
     */
    public static Long getLongConfig(String ckey) {
        String configVal = getStringConfig(ckey);
        if (StringUtils.isEmpty(configVal)) {
            return null;
        }
        Long ival = null;
        try {
            ival = Long.parseLong(configVal);
        } catch (NumberFormatException e) {
            logger.error(ExceptionUtils.getErrorMessageWithNestedException(e));
        }
        return ival;
    }

    /**
     * 根据ckey值获取Float类型配置项
     *
     * @param ckey ckey
     * @return val
     */
    public static Float getFloatConfig(String ckey) {
        String configVal = getStringConfig(ckey);
        if (StringUtils.isEmpty(configVal)) {
            return null;
        }

        Float ival = null;
        try {
            ival = Float.parseFloat(configVal);
        } catch (NumberFormatException e) {
            logger.error(ExceptionUtils.getErrorMessageWithNestedException(e));
        }
        return ival;
    }

    /**
     * 根据ckey值获取Float类型配置项
     *
     * @param ckey ckey
     * @return val
     */
    public static Double getDoubleConfig(String ckey) {
        String configVal = getStringConfig(ckey);
        if (StringUtils.isEmpty(configVal)) {
            return null;
        }
        Double ival = null;
        try {
            ival = Double.parseDouble(configVal);
        } catch (NumberFormatException e) {
            logger.error(ExceptionUtils.getErrorMessageWithNestedException(e));
        }
        return ival;
    }

    /**
     * 根据ckey值获取Map类型配置项
     *
     * @param ckey ckey
     * @return val
     */
    public static JSONObject getMapConfig(String ckey) {
        String strConf = getStringConfig(ckey);
        if (StringUtils.isEmpty(strConf)) {
            return new JSONObject();
        }
        return JsonUtils.parseObject(strConf);
    }

    /**
     * 获取 ConfigService实例
     *
     * @return ConfigService
     */
    public static ConsoleConfigService getConfigService() {
        if (null == configService) {
            return configService = SpringUtils.getBean(ConsoleConfigService.class);
        }
        return configService;
    }

    /**
     * 获取 RedisCacheService实例
     *
     * @return ConfigService
     */
    public static RedisCacheService getRedisCacheService() {
        if (null == redisCacheService) {
            return redisCacheService = SpringUtils.getBean(RedisCacheService.class);
        }
        return redisCacheService;
    }


}
